using UnityEngine;
using System.Collections;

public class spawnSoccerMoms : MonoBehaviour {
	
	public Transform rightSpawn;
	public Transform leftSpawn;
	public GameObject leftCar;
	public GameObject rightCar;
	public GameObject shootingEnemies;
	public AudioClip miniVanSound;
	
	int phase = 0;
	public GameObject eTrigger;
	
	float spawnTimer = 0;
	float encounterTimer = 0;
	float spawnRate = 2;
	float encounterLength = 30; // 30; TESTING ONLY
	bool spawning = true;
	
	
	// Use this for initialization
	void Start () {
		audio.clip = miniVanSound;
	}
	
	// Update is called once per frame
	void Update () {
		if(phase == 0){
			
			if(eTrigger.GetComponent<momEncounterTrigger>().firstTrigger){
				leftCar.transform.Translate(Vector3.down * 40,Space.World);
				StartCoroutine(playSound());
				phase = 1;
			}
		}
		if(phase == 1){
			if(eTrigger.GetComponent<momEncounterTrigger>().secondTrigger){
				shootingEnemies.SetActive(false);
				rightCar.transform.Translate(Vector3.down * 40,Space.World);
				StartCoroutine(playSound());
				phase = 2;
			}
		}
		if(phase == 2){
		
			spawnTimer += Time.deltaTime;
			encounterTimer += Time.deltaTime;
			if(spawnTimer > spawnRate && spawning){
				GameObject r = GenerateObjects.cheatMakeEnemyTwo(rightSpawn.position);
				GameObject l = GenerateObjects.cheatMakeEnemyTwo(leftSpawn.position);
				r.GetComponent<MeleeMove>().xDistToActivate = 50;
				l.GetComponent<MeleeMove>().xDistToActivate = 50;
				r.GetComponent<TakeMuliHits>().shotsToKill = 1;
				l.GetComponent<TakeMuliHits>().shotsToKill = 1;
				spawnTimer = 0;
				
				// remove these so they don't all respawn at death
				// OR remove script from prefab and add directly to on screen units
				GenerateObjects.everyEnemy.RemoveAt( GenerateObjects.everyEnemy.Count - 1 );
				GenerateObjects.everyEnemy.RemoveAt( GenerateObjects.everyEnemy.Count - 1 );
				
				GenerateObjects.originalLocations.RemoveAt( GenerateObjects.everyEnemy.Count - 1 );
				GenerateObjects.originalTags.RemoveAt( GenerateObjects.everyEnemy.Count - 1 );
			}
			
			if(encounterTimer > encounterLength){
				rightCar.transform.Translate(Vector3.left,Space.Self);
				spawning = false;
				
				// turn our shooters back on
				shootingEnemies.SetActive( true );
			}
		}
	}
	public IEnumerator playSound(){
		audio.clip = miniVanSound;
		audio.Play();
		
		yield return new WaitForSeconds(2);
		
	}
}
